<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admincp extends MX_Controller {
	
	function __construct(){
		parent::__construct();
		if($this->uri->segment(2)!='login'){
			if(!$this->session->userdata('userInfo')){
				header('Location: '.base_url().'admincp/login');
				return false;
			}
		}
		$this->load->model('admincp_model','model');
	
		$this->template->set_template('admincontrolpanel');
		$this->template->write('title','Admin Control Panel');
	}
	
	function index(){
		$data['module'] = 'admincp';
		$this->template->write_view('content','index',$data);
		$this->template->render();
	}
	
	function login(){
		if(!empty($_POST)){
			$username = htmlentities($this->input->post('username'),ENT_QUOTES,'UTF-8');
			$password = md5($this->input->post('password'));
			$user = $this->model->checkLogin($username,$password);
			
			if($user && $user[0]->id){
				$this->session->set_userdata('userInfo',$user[0]->username);
				$this->session->set_userdata('userId',$user[0]->id);
				header('Location: '.base_url().'admincp');
				exit();
			}else{
				$this->load->view('BACKEND/login');
			}
		}else{
			$this->load->view('BACKEND/login');
		}
	}
	
	function logout(){
		$this->session->unset_userdata('userInfo');
		header('Location: '.base_url().'admincp/login');
	}
}